package com.ruoyi.base.utils.unit;

import com.ruoyi.base.enums.ExceptionEnum;
import com.ruoyi.base.exception.SysException;
import com.ruoyi.base.utils.number.NumberUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Objects;

/**
 * 单位转换类
 *
 * @author lcf
 */
public class UnitUtils {
    /**
     * 元转分
     */
    public static int yuan2fen(BigDecimal yuan) {
        if (NumberUtils.isEmpty(yuan)) {
            return 0;
        }

        return yuan.multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.DOWN).intValue();
    }

    /**
     * 分转元
     */
    public static BigDecimal fen2yuan(Integer fen) {
        if (NumberUtils.isEmpty(fen)) {
            return BigDecimal.ZERO;
        }

        return BigDecimal.valueOf(fen).divide(BigDecimal.valueOf(100), 2, RoundingMode.DOWN);
    }

    /**
     * 根据传入大小返回合适的汉字展示
     */
    public static String getSizeStr(BigDecimal decimal, SizeUnitEnum sizeUnitEnum) {
        if (decimal == null || decimal.compareTo(BigDecimal.ZERO) == 0) {
            return "未占用";
        }

        BigDecimal low = new BigDecimal(1);
        BigDecimal high = new BigDecimal(1024);
        if (decimal.compareTo(low) >= 0 && decimal.compareTo(high) <= 0) {
            return decimal + sizeUnitEnum.getUnit();
        }

        if (decimal.compareTo(high) > 0) {
            SizeUnitEnum upperUnit = SizeUnitEnum.getByLevel(sizeUnitEnum.getLevel() + 1);
            if (upperUnit == null) {
                return decimal + sizeUnitEnum.getUnit();
            }

            decimal = decimal.divide(high, 3, BigDecimal.ROUND_UP);

            return getSizeStr(decimal, upperUnit);
        }

        if (decimal.compareTo(low) < 0) {
            SizeUnitEnum lowerUnit = SizeUnitEnum.getByLevel(sizeUnitEnum.getLevel() - 1);
            if (lowerUnit == null) {
                return decimal + sizeUnitEnum.getUnit();
            }

            decimal = decimal.multiply(high);

            return getSizeStr(decimal, lowerUnit);
        }

        return decimal + sizeUnitEnum.getUnit();
    }

    /**
     * 根据传入的大小返回指定单位的大小
     */
    public static BigDecimal changeSize2OtherUnit(BigDecimal decimal, SizeUnitEnum sizeUnitEnum, SizeUnitEnum otherSize) {
        if (Objects.equals(sizeUnitEnum.getLevel(), otherSize.getLevel())) {
            return decimal;
        }

        if (sizeUnitEnum.getLevel() > otherSize.getLevel()) {
            decimal = decimal.multiply(new BigDecimal(1024));
            SizeUnitEnum higherLevelUnit = SizeUnitEnum.getByLevel(sizeUnitEnum.getLevel() - 1);
            if (higherLevelUnit == null) {
                throw new SysException(ExceptionEnum.FILE_SIZE_CHANGE_ERROR);
            }

            return changeSize2OtherUnit(decimal, higherLevelUnit, otherSize);
        }

        decimal = decimal.divide(new BigDecimal(1024), 3, BigDecimal.ROUND_UP);
        SizeUnitEnum lowerLevelUnit = SizeUnitEnum.getByLevel(sizeUnitEnum.getLevel() + 1);
        if (lowerLevelUnit == null) {
            throw new SysException(ExceptionEnum.FILE_SIZE_CHANGE_ERROR);
        }

        return changeSize2OtherUnit(decimal, lowerLevelUnit, otherSize);
    }

    /**
     * 获取指定时间距今多久
     */
    public static String getTimeLongSince(Date date) {
        long time = System.currentTimeMillis() - date.getTime();
        if (time < 0) {
            throw new SysException("时间大于当前日期");
        }

        long seconds = time / 1000;
        if (seconds < 60) {
            return seconds + "秒";
        }

        long min = seconds / 60;
        if (min < 60) {
            return min + "分钟";
        }

        long hour = min / 60;
        if (hour < 24) {
            return hour + "小时";
        }

        long day = hour / 24;
        return day + "天";
    }

    public static void main(String[] args) {
        System.out.println(changeSize2OtherUnit(new BigDecimal(200), SizeUnitEnum.TB, SizeUnitEnum.B));
    }
}
